package cn.smxy.college.controller;

import cn.smxy.college.service.CollegeService;
import cn.smxy.param.CollegeDetailParam;
import cn.smxy.param.CollegeSearchParam;
import cn.smxy.param.PageParam;
import cn.smxy.pojo.College;
import cn.smxy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    private CollegeService collegeService;

    /**
     * 展示所有学校接口
     * @return
     */
    @GetMapping("/list")
    public R list() {
        return collegeService.list();
    }

    /**
     * 分页展示学校接口
     * @param collegeSearchParam 分页参数
     * @return
     */
    @PostMapping("/page")
    public R page(@RequestBody CollegeSearchParam collegeSearchParam) {
        return collegeService.page(collegeSearchParam);
    }

    /**
     * 获取学校信息详情接口
     * @param collegeDetailParam 学校名称
     * @param result 参数校验
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody @Validated CollegeDetailParam collegeDetailParam, BindingResult result) {
        if(result.hasErrors()) {
            return R.fail("参数异常，获取失败");
        }
        return collegeService.detail(collegeDetailParam);
    }

    // TODO: 管理员模块

    /**
     * 添加新学校接口
     * @param college
     * @param result
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody @Validated College college,BindingResult result) {
        if(result.hasErrors()) {
            return R.fail("参数异常，添加失败");
        }
        return collegeService.save(college);
    }

    /**
     * 修改学校信息接口
     * @param college
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody College college) {
        return collegeService.update(college);
    }

    /**
     * 删除学校接口
     * @param collegeId
     * @return
     */
    @DeleteMapping("/remove/{collegeId}")
    public R remove(@PathVariable("collegeId") Long collegeId) {
        return collegeService.remove(collegeId);
    }
}
